package com.ydy.controller.center;

import com.ydy.enums.YesOrNo;
import com.ydy.pojo.OrderItems;
import com.ydy.pojo.Orders;
import com.ydy.pojo.bo.center.OrderItemsCommentBO;
import com.ydy.service.center.MyCommentsService;
import com.ydy.service.center.MyOrdersService;
import com.ydy.utils.PageInfo;
import com.ydy.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("myComments")
public class MyCommentsController {

    @Autowired
    private MyOrdersService myOrdersService;

    @Autowired
    private MyCommentsService myCommentsService;

    /**
     * 查询订单列表
     * @param userId 用户id
     * @param orderId 订单id
     * @return
     */
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public ReturnResult pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) {

        // 判断用户和订单是否关联
        Orders orders = myOrdersService.checkUserOrder(userId, orderId);
        if (orders == null ) {
            return ReturnResult.errorMsg("fuck you");
        }

        // 判断该笔订单是否已经评价过，评价过了就不再继续
        if (orders.getIsComment() == YesOrNo.YES.type) {
            return ReturnResult.errorMsg("该笔订单已经评价");
        }

        List<OrderItems> list = myCommentsService.queryPendingCommentItems(orderId);

        return ReturnResult.ok(list);
    }


    /**
     * 保存评论列表
     * @param userId 用户id
     * @param orderId 订单id
     * @param commentList 评论集合
     * @return
     */
    @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public ReturnResult saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList) {

        // 判断用户和订单是否关联
        Orders orders = myOrdersService.checkUserOrder(userId, orderId);
        if (orders == null ) {
            return ReturnResult.errorMsg("fuck you");
        }

        if (commentList == null || commentList.isEmpty() || commentList.size() == 0) {
            return ReturnResult.errorMsg("评论内容不能为空！");
        } else {
            myCommentsService.saveComments(orderId, userId, commentList);
            return ReturnResult.ok();
        }
    }

    /**
     * 查询我的评价
     * @param userId 用户id
     * @param page 当前页
     * @param pageSize 每页条数
     * @return
     */
    @ApiOperation(value = "查询我的评价", notes = "查询我的评价", httpMethod = "POST")
    @PostMapping("/query")
    public ReturnResult query(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            return ReturnResult.errorMsg("fuck you");
        }

        PageInfo pageInfo = new PageInfo(page,pageSize);
        List grid = myCommentsService.queryMyComments(userId,pageInfo);
        pageInfo.setPageResult(grid);
        return ReturnResult.ok(pageInfo);
    }
}
